package com.fengwan.wastesort.net

import okhttp3.OkHttpClient
import okhttp3.logging.HttpLoggingInterceptor
import retrofit2.Retrofit
import retrofit2.adapter.rxjava2.RxJava2CallAdapterFactory
import retrofit2.converter.gson.GsonConverterFactory
import java.util.*
import java.util.concurrent.TimeUnit

/**
 * @author : Silence丶J
 * @time : 2019/7/19
 * @description :
 */
class HttpUtil {

    private val TIME_OUT = 10
    private var RETROFIT_CLIENT: Retrofit

    init {
//        val BASE_URL: String = Core.getConfigurations()[ConfigType.API_HOST.name] as String

        val HTTP_CLIENT = OkHttpClient.Builder()
            .connectTimeout(TIME_OUT.toLong(), TimeUnit.SECONDS)
            .readTimeout(TIME_OUT.toLong(), TimeUnit.SECONDS)
            .writeTimeout(TIME_OUT.toLong(), TimeUnit.SECONDS)
            .addInterceptor(HttpLoggingInterceptor().setLevel(HttpLoggingInterceptor.Level.BODY))
            .build()

        RETROFIT_CLIENT = Retrofit.Builder()
            .baseUrl("http://www.baidu.com")
            .addCallAdapterFactory(RxJava2CallAdapterFactory.create())
            .addConverterFactory(GsonConverterFactory.create())
            .client(HTTP_CLIENT)
            .build()
    }

    companion object {

        private object PARAMS_HOLDER {
            val PARAMS = WeakHashMap<String, Any>()
        }

        fun getParams(): WeakHashMap<String, Any> {
            return PARAMS_HOLDER.PARAMS
        }

        private object Holder {
            val INSTANCE = HttpUtil()
        }

        fun getInstance() = Holder.INSTANCE
    }

    fun <T> getService(clazz: Class<T>): T {
        return RETROFIT_CLIENT.create(clazz)
    }

    fun getBaiduService(): BaiduAiApi {
        return RETROFIT_CLIENT.create(BaiduAiApi::class.java)
    }

}